#include <stdio.h>
#include <iostream>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <vector>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <tuple>
#include <sys/io.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/times.h>
#include <fcntl.h>
#include <linux/fb.h>

using namespace std;

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;

// cvui git: https://github.com/Dovyski/cvui.git
// you only need to include "cvui.h", no need lib or other.
#define CVUI_IMPLEMENTATION
#include "cvui.h"

/******************************
* UI Design
*/
#define WINDOW_NAME "Tool - Make Samples"
const cv::Size win_size = cv::Size(1800, 1080); // for screen resolution 1920*1200
cv::Rect rctForImg(0, 0, win_size.width - 320, win_size.height);
cv::Rect rctForCP(win_size.width - 320 - 1, 0, 320, win_size.height);
cv::Rect rctForAlgType(rctForCP.x, 
                       rctForCP.y, 
                       rctForCP.width, 
                       300);
cv::Rect rctForAlgFunc(rctForAlgType.x, 
                       rctForAlgType.y + rctForAlgType.height, 
                       rctForAlgType.width, 
                       rctForCP.height - rctForAlgType.y - rctForAlgType.height);
// image for showing
cv::Mat frame; 

/******************************
* Main
*/
int main(int argc, char** argv)
{
	cout << "# STA ##############################" << endl;
    cout << "\n" << endl;
    if (win_size.width <= 600 || win_size.height <= 480) {
        cout << ">> error. win_size = " << win_size << endl;
        return 0;
    }
    // Init cvui and tell it to create a OpenCV window, i.e. cv::namedWindow(WINDOW_NAME).
	frame = cv::Mat(win_size, CV_8UC3);
	cvui::init(WINDOW_NAME);
	while (true) {
	// Fill the background with a nice color first
	frame = cv::Scalar(49, 52, 49);
    cvui::window(frame, 
                 rctForAlgType.x, 
                 rctForAlgType.y, 
                 rctForAlgType.width, 
                 rctForAlgType.height, "Select One Methods");
    cvui::window(frame, 
                 rctForAlgFunc.x, 
                 rctForAlgFunc.y, 
                 rctForAlgFunc.width, 
                 rctForAlgFunc.height, "Function List");
	
	// This function must be called *AFTER* all UI components. It does
	// all the behind the scenes magic to handle mouse clicks, etc.
	cvui::update();
	// Show everything on the screen
	cv::imshow(WINDOW_NAME, frame);
	// Check if ESC key was pressed
	if (cv::waitKey(20) == 27) {
		break;
	}
	}
    
    cout << "\n" << endl;
    cout << "# END ##############################" << endl;
    return 0;
}

int paste_image(cv::Mat &src) {
    if (src.empty()) {
        cout << ">> error. paste_image() src is empty" << endl;
        return 1;
    }
    
    if (src.rows <= rctForImg.height && src.cols <= rctForImg.width) {
        cv::Rect rctTmp(rctForImg.width / 2 - src.cols / 2, 
                        rctForImg.height/ 2 - src.rows / 2,
                        src.cols,
                        src.rows);
    }
    else { // need to resize the image
    
    }
    return 0;
}































